/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import business.BankBusiness;
import business.CarBusiness;
import business.TourBusiness;
import java.io.Serializable;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import model.CarInCart;
import model.Customer;
import ws.Bank;

/**
 *
 * @author khanhanh
 */
public class BankBean implements Serializable {

    private HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    private BankBusiness bankBiz;
    private TourBusiness tourBiz;
    private CarBusiness carBiz;
    private String ccNumber;
    private String pinCode;
    private float balance = 0;
    private Bank bank;

    public String getCcNumber() {
        return ccNumber;
    }

    public void setCcNumber(String ccNumber) {
        this.ccNumber = ccNumber;
    }

    public float getBalance() {
        return balance;
    }

    public String getPinCode() {
        return pinCode;
    }

    public void setPinCode(String pinCode) {
        this.pinCode = pinCode;
    }

    /** Creates a new instance of BankBean */
    public BankBean() {
        bankBiz = new BankBusiness();
        tourBiz = new TourBusiness();
        carBiz = new CarBusiness();
    }

    public String actionGetBalance() {
        bank = bankBiz.getBank(ccNumber, pinCode);
        if (bank != null) {
            balance = bank.getBalance();
        } else {
            balance = 0;
        }

        return "ordertour.xhtml";
    }

    public String actionCreateTourBooking(int packageTourID, int numbOfPeople, float totalPrice) {
        Customer customer = (Customer) session.getAttribute("customer");
        if (balance < totalPrice) {
            return "ordertour.xhtml";
        } else {
            int customerID = customer.getCustomerID();
            tourBiz.createTourBooking(customerID, packageTourID, numbOfPeople, totalPrice);
            bankBiz.withdraw_bank(ccNumber, pinCode, totalPrice);
            bankBiz.deposit_bank("0000000000", "1234", totalPrice);
            return "finishorder.xhtml";
        }
    }

    public String actionCreateCarBooking(List<CarInCart> cartCar) {
        Customer customer = (Customer) session.getAttribute("customer");
        float total = 0;
        for (CarInCart cIc : cartCar) {
            total += cIc.getTotalPerCar();
        }

        if (balance<total) {
            return "ordercar.xhtml";
        }else{
            int customerID = customer.getCustomerID();
            carBiz.createCarBooking(customerID, cartCar);
            bankBiz.withdraw_bank(ccNumber, pinCode, total);
            bankBiz.deposit_bank("0000000000", "1234", total);
            return "finishorder.xhtml";
        }
    }
}
